# 3.7 寻找最优超参数
from sklearn import svm
from sklearn import model_selection
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import classification_report
import pandas as pd
import numpy as np


def load_data(input_file):
    X = []
    y = []
    with open(input_file,'r') as f:
        for line in f.readlines():
            data = [float(x) for x in line.split(',')]
            X.append(data[:-1])
            y.append(data[-1])

    X = np.array(X)
    y = np.array(y)
    return X,y

input_file = "F:/python学习资料/Python-Machine-Learning-Cookbook-master/" \
             "Chapter03/data_multivar.txt"

X,y = load_data(input_file)
# print(X[:8])
# print(y[:8])

X_train,X_test,y_train,y_test = model_selection.train_test_split(X,y,test_size=0.25,random_state=5)
# 设置交叉验证参数
parameter_grid = {"C":[1,10,50,600],
                  'kernel':['linear','poly','rbf'],
                  'gamma':[0.01,0.001],
                  'degree':[2,3]
                  }

# 接下来定义性能指标
metrics = ['precision']

for metric in metrics:
    print("### 网格搜索，衡量指标为",metric)
    classifier = GridSearchCV(svm.SVC(C=1),parameter_grid,cv=5,scoring=metric,return_train_score=True)
    classifier.fit(X_train,y_train)
    # 查看指标得分
    print("Scores across the parameter grid:")
    GridSearchResults = pd.DataFrame(classifier.cv_results_)
    for i in range(0,len(GridSearchResults)):
        # cv_results中的params保存的是所有的参数组合
        print(GridSearchResults.params[i],'-->',round(GridSearchResults.mean_test_score[i],3))

    print("最佳参数组合:",classifier.best_params_)

    # 使用测试数据集评估模型
    y_true,y_pred = y_test,classifier.predict(X_test)
    print("最佳参数组合在测试集上的表现")
    print(classification_report(y_true,y_pred))